const path = require("path");
const { ModuleFederationPlugin } = require("webpack").container;
const deps = require("../package.json").dependencies;
const {
  SanRemoteModulePlugin,
  ListenAssetsJsPlugin,
  updateAppInfo,
} = require("../../base/config/plugins");
module.exports = {
  output: {
    clean: true,
    path: path.resolve(__dirname, "../dist"),
    filename: '[name].[contenthash:8].bundle.js',
    chunkFilename: '[name].[contenthash:8].chuck.js',
  },
  resolve: {
    extensions: [".jsx", ".js", ".ts", ".tsx"],
    alias: {
      "@": path.resolve(__dirname, "../src"),
    },
  },
  module: {
    rules: [
      {
        test: /\.[jt]sx?$/,
        exclude: [/node_modules/],
        use: [
          {
            loader: "babel-loader",
            options: {
              rootMode: "upward",
            },
          },
        ],
      },
      {
        test: /\.css$/,
        use: ["style-loader", "css-loader"],
      },
    ],
  },
  plugins: [
    new ModuleFederationPlugin({
      name: "common",
      filename: "entry.[contenthash:8].js",
      exposes: {
        "./utils/watcher": "@/utils/watcher",
        "./components/test": "@/components/test",
        "./react": "react",
        "./react-dom": "react-dom",
        "./react-router-dom": "react-router-dom",
      },
    }),
    new SanRemoteModulePlugin(),
    new ListenAssetsJsPlugin((file) => {
      if (file.startsWith("entry")) {
        const fileContext = "/common/"; //"http://localhost:4020/"
        updateAppInfo("common", fileContext + file);
        return true;
      }
    }),
  ],
};
